package com.wisemoresystem.seal.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.page.Page;
import org.ne.framework.data.task.Task;
import org.ne.framework.data.util.ChkTools;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.model.t_mac;
import com.wisemoresystem.seal.web.WebTools;

@Service
public class MacService extends BaseService {

	// 检查是否存在 mac id
	public List<String> checkIsHave(String[] macCodes) {
		List<String> retMacCodes = new ArrayList<String>();

		String hql = "from t_mac t where t.code=? and t.status=0";
		for (String code : macCodes) {
			t_mac mac = super.findObj(hql, code);
			if (mac != null) {
				retMacCodes.add(code);
			}
		}

		return retMacCodes;
	}

	/*
	 * @Task(code = "controlled5", tip = "待分发的印控仪(@cnt)") public long
	 * selectInStockList() {
	 * 
	 * String sql = "select count(*) cnt from t_mac t " +
	 * " where t.flag='停用' and t.flag_stop='在库' and t.status=0 and t.bank_id=? "
	 * + " order by t.created desc";
	 * 
	 * Long cnt = (Long) jt.queryForMap(sql, WebTools.getCurrentBankId()).get(
	 * "cnt"); return cnt; }
	 */

	// 查询 停用 在库 的列表
	public List<Map<String, Object>> selectInStockList(String srh_name,
			Page page) {

		String sql = "select t.*,tu.code ucode,tb.code bcode,tb.name bname from t_mac t "
				+ " left outer join t_user tu on t.user_id=tu._id "
				+ " left outer join t_mac_move_log tlog on t._id=tlog.mac_id and tlog.type='录入' "
				+ " left outer join t_bank tb on tlog.bank_id=tb._id "
				+ " where t.code like ? and t.bank_id=? and t.flag='停用' and t.flag_stop='在库' and t.status=0 "
				+ " order by t.created desc";

		List<String> params = new ArrayList<String>();
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("t.code like ? and ", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		params.add(WebTools.getCurrentBankId());
		List<Map<String, Object>> list = jc.queryForList(sql, params.toArray(),
				page);

		return list;

	}

	// 查询所有的列表
	public List<Map<String, Object>> selectAllList(String srh_name,
			String bank_id, Page page) {

		String sql = "select t.*,tu.code ucode,tu.uname,tb.code bcode,tb.name bname from t_mac t "
				+ " left outer join t_user tu on t.user_id=tu._id "
				+ " left outer join t_bank tb on t.bank_id=tb._id "
				+ " where t.code like ? and t.bank_id=? and t.status=0 "
				+ " order by t.created desc";

		List<String> params = new ArrayList<String>();
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("t.code like ? and ", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		params.add(bank_id);

		List<Map<String, Object>> list = jc.queryForList(sql, params.toArray(),
				page);

		return list;

	}

	// 下发中 的仪器列表
	@Task(code = "controlled2", tip = "待接收的印控仪(@cnt)")
	public long selectInRecvingList() {

		String sql = "select count(*) cnt from t_mac t "
				+ " left outer join t_mac_move_log tlog on tlog.status=0 and t._id=tlog.mac_id and tlog.type='分发' and tlog.is_end='否' "
				+ " where t.flag='停用' and t.flag_stop='分发中' and t.status=0 and tlog.bank_id_dest=?  ";
		
		Long cnt = super.getTaskCnt(sql, WebTools.getCurrentBankId());
		return cnt;
	}

	// 下发中 的仪器列表
	public List<Map<String, Object>> selectInRecvingList(String srh_name,
			Page page) {

		String sql = "select t.*,tu.code ucode,tb.code bcode,tb.name bname from t_mac t "
				+ " left outer join t_user tu on t.user_id=tu._id "
				+ " left outer join t_mac_move_log tlog on tlog.status=0 and t._id=tlog.mac_id and tlog.type='分发' and tlog.is_end='否' "
				+ " left outer join t_bank tb on tlog.bank_id=tb._id "
				+ " where t.code like ? and t.flag='停用' and t.flag_stop='分发中' and t.status=0 and tlog.bank_id_dest=? "
				+ " order by t.created desc";
		List<String> params = new ArrayList<String>();
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("t.code like ? and ", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		params.add(WebTools.getCurrentBankId());
		List<Map<String, Object>> list = jc.queryForList(sql, params.toArray(),
				page);

		return list;
	}

	// 停用 中 的印控仪
	public List<Map<String, Object>> selectStopingList(String srh_name,
			Page page) {
		String sql = "select t.*,tu.code ucode,tb.code bcode,tb.name bname from t_mac t "
				+ " left outer join t_user tu on t.user_id=tu._id "
				+ " left outer join t_bank tb on t.bank_id=tb._id "
				+ " where t.code like ? and t.bank_id=? and t.flag='停用' and t.flag_stop='在库' and t.status=0 "
				+ " order by t.created desc";

		List<String> params = new ArrayList<String>();
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("t.code like ? and ", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		params.add(WebTools.getCurrentBankId());
		List<Map<String, Object>> list = jc.queryForList(sql, params.toArray(),
				page);

		return list;
	}

	// 查询 启用中 的印控仪
	public List<Map<String, Object>> selectStartingList(String srh_name,
			Page page) {

		String sql = "select t.*,tu.code ucode,tb.code bcode,tb.name bname from t_mac t "
				+ " left outer join t_user tu on t.user_id=tu._id "
				+ " left outer join t_bank tb on t.bank_id=tb._id "
				+ " where t.code like ? and t.bank_id=? and t.flag='启用' and t.status=0 "
				+ " order by t.created desc";
		List<String> params = new ArrayList<String>();
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("t.code like ? and ", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		params.add(WebTools.getCurrentBankId());
		List<Map<String, Object>> list = jc.queryForList(sql, params.toArray(),
				page);

		return list;

	}

	// 查询 调拨申请中 的印控仪
	@Task(code = "controlled8", tip = "待确认的印控仪调拨申请(@cnt)")
	public long selectTransferApplyedList() {

		String sql = "select count(*) cnt from t_mac t "
				+ " left outer join t_mac_move_log tlog on tlog.status=0 and t._id=tlog.mac_id and tlog.type='调拨' and tlog.is_end='否' "
				+ " where t.flag='停用' and t.flag_stop='调拨申请中' and t.status=0 and tlog.verify_bank_id=? ";

		Long cnt = super.getTaskCnt(sql, WebTools.getCurrentBankId());

		return cnt;

	}

	// 查询 调拨申请中 的印控仪
	public List<Map<String, Object>> selectTransferApplyedList(String srh_name,
			Page page) {

		String sql = "select t.*,tu.code ucode,tb.code bcode,tb.name bname,tb_dest.code bdcode,tb_dest.name bdname from t_mac t "
				+ " left outer join t_user tu on t.user_id=tu._id "
				+ " left outer join t_mac_move_log tlog on tlog.status=0 and t._id=tlog.mac_id and tlog.type='调拨' and tlog.is_end='否' "
				+ " left outer join t_bank tb on tlog.bank_id=tb._id "
				+ " left outer join t_bank tb_dest on tlog.bank_id_dest=tb_dest._id "
				+ " where t.code like ? and t.flag='停用' and t.flag_stop='调拨申请中' and t.status=0 and tlog.verify_bank_id=? "
				+ " order by t.created desc";

		List<String> params = new ArrayList<String>();
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("t.code like ? and ", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		params.add(WebTools.getCurrentBankId());
		List<Map<String, Object>> list = jc.queryForList(sql, params.toArray(),
				page);

		return list;

	}

	@Task(code = "controlled13", tip = "未完成的印控仪调拨确认(@cnt)")
	public long selectTransferRecvingList() {
		String sql = "select count(*) cnt from t_mac t "
				+ " left outer join t_mac_move_log tlog on tlog.status=0 and t._id=tlog.mac_id and tlog.type='调拨' and tlog.verify_flag='通过' and tlog.is_end='否' "
				+ " where  t.flag='停用' and t.flag_stop='调拨中' and t.status=0 and tlog.bank_id_dest=? ";

		Long cnt = super.getTaskCnt(sql, WebTools.getCurrentBankId());

		return cnt;
	}

	// 查询 调拨中 的印控仪
	public List<Map<String, Object>> selectTransferRecvingList(String srh_name,
			Page page) {
		String sql = "select t.*,tu.code ucode,tb.code bcode,tb.name bname from t_mac t "
				+ " left outer join t_user tu on t.user_id=tu._id "
				+ " left outer join t_mac_move_log tlog on tlog.status=0 and t._id=tlog.mac_id and tlog.type='调拨' and tlog.verify_flag='通过' and tlog.is_end='否' "
				+ " left outer join t_bank tb on tlog.bank_id=tb._id "
				+ " left outer join t_bank tb_dest on tlog.bank_id_dest=tb_dest._id "
				+ " where t.code like ? and t.flag='停用' and t.flag_stop='调拨中' and t.status=0 and tlog.bank_id_dest=? "
				+ " order by t.created desc";

		List<String> params = new ArrayList<String>();
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("t.code like ? and ", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		params.add(WebTools.getCurrentBankId());
		List<Map<String, Object>> list = jc.queryForList(sql, params.toArray(),
				page);

		return list;
	}

	@Task(code = "controlled3", tip = "未完成的印控仪上缴停用申请(@cnt)")
	public long selectStopingToOverList() {
		String sql = "select count(*) cnt from t_mac t "
				+ " where t.flag='停用' and t.flag_stop='上缴停用' and t.status=0 and t.bank_id=? ";

		Long cnt = super.getTaskCnt(sql,WebTools.getCurrentBankId());
		return cnt;
	}

	// 查询 上缴停用 的印控仪
	public List<Map<String, Object>> selectStopingToOverList(String srh_name,
			Page page) {
		String sql = "select t.*,tu.code ucode,tb.code bcode,tb.name bname from t_mac t "
				+ " left outer join t_user tu on t.user_id=tu._id "
				+ " left outer join t_bank tb on t.bank_id=tb._id "
				+ " where t.code like ? and t.flag='停用' and t.flag_stop='上缴停用' and t.status=0 and t.bank_id=? "
				+ " order by t.created desc";
		List<String> params = new ArrayList<String>();
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("t.code like ? and ", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		params.add(WebTools.getCurrentBankId());
		List<Map<String, Object>> list = jc.queryForList(sql, params.toArray(),
				page);

		return list;
	}

	// 查询 上缴申请中 的印控仪
	@Task(code = "controlled1000", tip = "待上缴审批的印控仪(@cnt)")
	public long selectOverApplyedList() {

		String sql = "select count(*) cnt from t_mac t "
				+ " left outer join t_mac_move_log tlog on tlog.status=0 and t._id=tlog.mac_id and tlog.type='上缴' and tlog.is_end='否' "
				+ " where t.flag='停用' and t.flag_stop='上缴申请中' and t.status=0 and tlog.bank_id_dest=? ";

		Long cnt = super.getTaskCnt(sql, WebTools.getCurrentBankId());

		return cnt;
	}

	// 查询 上缴申请中 的印控仪
	public List<Map<String, Object>> selectOverApplyedList(String srh_name,
			Page page) {

		String sql = "select t.*,tu.code ucode,tb.code bcode,tb.name bname,tb_dest.code bdcode,tb_dest.name bdname from t_mac t "
				+ " left outer join t_user tu on t.user_id=tu._id "
				+ " left outer join t_mac_move_log tlog on tlog.status=0 and t._id=tlog.mac_id and tlog.type='上缴' and tlog.is_end='否' "
				+ " left outer join t_bank tb on tlog.bank_id=tb._id "
				+ " left outer join t_bank tb_dest on tlog.bank_id_dest=tb_dest._id "
				+ " where t.code like ? and t.flag='停用' and t.flag_stop='上缴申请中' and t.status=0 and tlog.bank_id_dest=? "
				+ " order by t.created desc";

		List<String> params = new ArrayList<String>();
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("t.code like ? and ", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		params.add(WebTools.getCurrentBankId());
		List<Map<String, Object>> list = jc.queryForList(sql, params.toArray(),
				page);

		return list;

	}

	@Task(code = "controlled10001", tip = "待接收的印控仪(@cnt)")
	public long selectOverRecvingList() {

		String sql = "select count(*) cnt from t_mac t "
				+ " left outer join t_mac_move_log tlog on tlog.status=0 and t._id=tlog.mac_id and tlog.type='上缴' and tlog.is_end='否' "
				+ " where t.flag='停用' and t.flag_stop='上缴中' and t.status=0 and tlog.bank_id_dest=? ";

		Long cnt = super.getTaskCnt(sql, WebTools.getCurrentBankId());
		return cnt;
	}

	// 查询 上缴中 的印控仪
	public List<Map<String, Object>> selectOverRecvingList(String srh_name,
			Page page) {
		String sql = "select t.*,tu.code ucode,tb.code bcode,tb.name bname from t_mac t "
				+ " left outer join t_user tu on t.user_id=tu._id "
				+ " left outer join t_mac_move_log tlog on tlog.status=0 and t._id=tlog.mac_id and tlog.type='上缴' and tlog.verify_flag='通过' and tlog.is_end='否' "
				+ " left outer join t_bank tb on tlog.bank_id=tb._id "
				+ " left outer join t_bank tb_dest on tlog.bank_id_dest=tb_dest._id "
				+ " where t.code like ? and t.flag='停用' and t.flag_stop='上缴中' and t.status=0 and tlog.bank_id_dest=? "
				+ " order by t.created desc";
		List<String> params = new ArrayList<String>();
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("t.code like ? and ", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		params.add(WebTools.getCurrentBankId());
		List<Map<String, Object>> list = jc.queryForList(sql, params.toArray(),
				page);
		return list;
	}

	// 查询设备列表
	public List<Map<String, Object>> selectMac(String bank_id) {
		String sql = "select t.* from t_mac t where t.bank_id=? and t.flag_stop=? and t.flag !=? and  t.status=0 ";

		List<Map<String, Object>> list = jt.queryForList(sql, bank_id, "在库",
				"报废");
		return list;
	}

	// 用户可用印章
	public List<Map<String, Object>> selectUserMac(String ids) {
		String sql = "select ts._id seal_id,tm._id mac_id,t.position,ts.name,ts.true_code,tc.user_way,tm.code mac_code,tm.mac,tm.mac_model"
				+ " ,tp.name seal_type,tc.code seal_code from t_seal_user_use t "
				+ " left join t_seal ts on ts._id=t.seal_id and t.status=0 "
				+ " left join t_mac tm on tm._id=t.mac_id and t.status=0 "
				+ " left join t_seal_new tn on tn._id=ts.seal_new_id and t.status=0 "
				+ " left join t_seal_type tp on tp._id=tn.seal_type_id and t.status=0 "
				+ " left join t_seal_type_config tc on tc._id=tn.seal_type_config_id and t.status=0 "
				+ " where t.status=0 and t.flag='已安装' and t.bank_id=? and t.use_user_id in("
				+ ids + ") ";
		List<String> params = new ArrayList<String>();
		params.add(WebTools.getCurrentBankId());

		List<Map<String, Object>> list = jt.queryForList(sql, params.toArray());
		return list;
	}
	
	// 核对印控仪
	public List<Map<String, Object>> chkUserMac(String mac_code) {
		String sql = "select t.* from t_mac t "
				+ " where t.status=0 and t.bank_id=? and t.code=? and t.flag_stop='在库' and t.flag='启用' ";
		
		List<String> params = new ArrayList<String>();
		params.add(WebTools.getCurrentBankId());
		params.add(mac_code);
		
		List<Map<String, Object>> list = jt.queryForList(sql, params.toArray());
		return list;
	}
	
	public static void main(String[] args) {
	}

	// 取消上缴
	@Transactional
	public boolean cancleMac(String mac_id) {
		t_mac mac = super.findObjById(mac_id, t_mac.class);
		mac.setFlag("停用");
		mac.setFlag_stop("在库");
		super.updateObj(mac);
		return true;
	}

	// 判断印控仪是否正在使用
	public boolean IsMacUsing(String mac_id) {
		String sql = "select * from t_seal_user_use t where t.status=0 "
				+ " and t.bank_id=? and t.flag='已安装' and t.mac_id=? ";
		List<Map<String, Object>> list = jt.queryForList(sql,
				WebTools.getCurrentBankId(), mac_id);
		if (list.size() > 0)
			return true;
		return false;
	}

	// 判断印控仪位置是否使用
	public String checkUniqueLocation(String mac_id, String position) {
		String sql = "select * from t_seal_user_use t where t.status=0 "
				+ " and t.bank_id=? and t.flag='已安装' and t.mac_id=? and t.position=? ";
		List<Map<String, Object>> list = jt.queryForList(sql,
				WebTools.getCurrentBankId(), mac_id, position);
		if (list.size() > 0)
			return "success";
		return "false";
	}

}
